package com.jiutong.meipengdai.ui.activitys;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.BaseRespond;
import com.jiutong.meipengdai.http.model.interfaces.MyAccountBiz;
import com.jiutong.meipengdai.http.present.MyAccountPresent;
import com.jiutong.meipengdai.views.SwitchView;
import com.jiutong.meipengdai.views.dialog.CitySelectedDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewAddressActivity extends BaseActivity implements MyAccountBiz {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_selecte_area)
    RelativeLayout rlSelecteArea;
    @Bind(R.id.et_address_name)
    EditText etAddressName;
    @Bind(R.id.et_address_num)
    EditText etAddressNum;
    @Bind(R.id.et_address_location)
    EditText etAddressLocation;
    @Bind(R.id.tv_location)
    TextView tvLocation;
    @Bind(R.id.sv_isdefault)
    SwitchView svIsDefault;
    @Bind(R.id.btn_save)
    Button btnSave;
    //    private String name;
//    private String num;
//    private String provience;
    private String isDefault;
    private CitySelectedDialog citySelectedDialog;
    private MyAccountPresent present;
    //    private String detail;
//    private String city;
//    private String district;
//    private String id;
    private String location;


    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_edit_address;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        present = new MyAccountPresent(this, this);
        tvTitle.setText("收货地址");

    }


    @OnClick({R.id.iv_back, R.id.rl_selecte_area, R.id.sv_isdefault, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_selecte_area:
                //TODO
                cityList();
                break;
            case R.id.sv_isdefault:

                break;
            case R.id.btn_save:
                if (TextUtils.isEmpty(etAddressName.getText().toString()) || TextUtils.isEmpty(etAddressNum.getText().toString()) ||
                        TextUtils.isEmpty(tvLocation.getText().toString()) || TextUtils.isEmpty(etAddressLocation.getText().toString())) {
                    toastShortShow("内容必须填写完整!");
                } else {
                    present.saveNewAddress();
                }
                break;
        }
    }

    //城市列表选择器
    private void cityList() {
        if (citySelectedDialog == null) {
            citySelectedDialog = new CitySelectedDialog();
            citySelectedDialog.setOnDialogClickListener(new CitySelectedDialog.OnDialogClickListener() {
                @Override
                public void onItemClick(String province, String city, String area) {
//                    toastShortShow(province + city + area);
                    tvLocation.setText(province + city + area);
                    tvLocation.setTextColor(getResources().getColor(R.color.defalult_black));
                    location = province + "-" + city + "-" + area;
                }
            });
        }
        citySelectedDialog.show(getSupportFragmentManager(), "EditAddressActivity");
    }

    @Override
    public String getBankNum() {
        return null;
    }

    @Override
    public String getBankName() {
        return null;
    }

    @Override
    public String getCartStatus() {
        return null;
    }

    @Override
    public String getBankId() {
        return null;
    }

    @Override
    public String getMsgId() {
        return null;
    }

    @Override
    public String getLoanId() {
        return null;
    }

    @Override
    public String Consignee() {
        return etAddressName.getText().toString();
    }

    @Override
    public String getTelephone() {
        return etAddressNum.getText().toString();
    }

    @Override
    public String getBigLocation() {
        return location;
    }

    @Override
    public String getDetail() {
        return etAddressLocation.getText().toString();
    }

    @Override
    public String getIsDefault() {
        if (svIsDefault.isOpened()) {
            isDefault = "1";
        } else {
            isDefault = "0";
        }
        return isDefault;
    }

    @Override
    public String getOprType() {
        return null;
    }

    @Override
    public String getMainId() {
        return null;
    }

    @Override
    public void onSuccess(Object object) {

        BaseRespond respond = (BaseRespond) object;
        if(!"200".equals(respond.getResCode())){
            toastShortShow(respond.getResDesc());
        }else{
            finish();
        }

    }

    @Override
    public void onError(Throwable e) {

    }

}
